package com.example.opengldemo.jbulletJava;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

import android.opengl.GLES30;
import android.util.Log;
import android.view.View;

import com.example.opengldemo.util.MatrixState;
import com.example.opengldemo.util.ShaderManager;
import com.example.opengldemo.util.ShaderUtil;

public class TextureRect {


    SimpleShaderHandle shaderHandle;
    SimpleShaderBuffer shaderBuffer;
    float width;
    float height;


    public TextureRect(final float UNIT_SIZE, float width, float height) {
        this();
        this.width = width;
        this.height = height;
        initVertexData(UNIT_SIZE);
    }

    public TextureRect(final float UNIT_SIZE) {
        this(UNIT_SIZE, 1, 1);
    }

    public TextureRect() {
        shaderBuffer = new SimpleShaderBuffer();
    }

    public void initVertexData(final float UNIT_SIZE) {

        float vertices[] = new float[]
                {
                        -width * UNIT_SIZE, height * UNIT_SIZE, 0,
                        -width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, height * UNIT_SIZE, 0,

                        -width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, -height * UNIT_SIZE, 0,
                        width * UNIT_SIZE, height * UNIT_SIZE, 0
                };

        shaderBuffer.setVertexBuffer(vertices);

        float textures[] = new float[]
                {
                        0, 1, 0, 0, 1, 1,
                        0, 0, 1, 0, 1, 1
                };
        float textures2[] = new float[]
                {
                        0, 0, 0, 1, 1, 0,
                        0, 1, 1, 1, 1, 0
                };
        shaderBuffer.setTexcoorBuffer(textures);
    }

    public void setVertexBuffer(float[] floats) {
        shaderBuffer.setVertexBuffer(floats);
    }

    public void setTexCoorBuffer(float[] floats) {
        shaderBuffer.setTexcoorBuffer(floats);
    }

    public void initShader(int mProgram) {
        Log.e("intShader 1" , "program > "+mProgram) ;
        shaderHandle = new SimpleShaderHandle(mProgram);
    }

    public void initShader(View mv, int mProgram) {
        if(mProgram >= 0){
            this.initShader(mProgram);
            return ;
        }
        String mVertexShader = ShaderUtil.loadFromAssetsFile("jbullet_java/vertex.sh", mv.getResources());
        String mFragmentShader = ShaderUtil.loadFromAssetsFile("jbullet_java/frag.sh", mv.getResources());
        this.initShader(ShaderUtil.createProgram(mVertexShader, mFragmentShader));
    }


    public void drawSelf(int texId) {
        initShader(ShaderManager.getTextureShaderProgram());
        if (shaderHandle == null || shaderBuffer == null) return;
        shaderHandle.program()
                .bindMVPMaxtrixHandle(MatrixState.getFinalMatrix())
                .bindMMaxtrixHandle(MatrixState.getMMatrix())
                .bindCameraHandle(MatrixState.cameraLocationFB)
                .bindVertexHandle(shaderBuffer.getVertexBuffer())
                .bindTexCoorsHandle(shaderBuffer.getTexcoorBuffer())
                .bindDefaultTextureHandle(texId)
                .run(GLES30.GL_TRIANGLES, shaderBuffer.vCount);
    }
}
